/****************************************************************************
**
** Copyright (C) 2009-2010 Mist Poryvaev.
** All rights reserved.
** Contact: Mist Poryvaev (mist.poryvaev@gmail.com)
**
** This file is part of the RailWorm project.
**
****************************************************************************/

#include "setuplogon.h"
#include "ui_setuplogon.h"
#include "connectrzd.h"

QSettings SetupLogonSettings("railworm.ini", QSettings::IniFormat);

SetupLogon * SetupLogon::g_Instance = 0;

SetupLogon * SetupLogon::i() {
	if (g_Instance == 0) {
		g_Instance = new SetupLogon();
	}
	return g_Instance;
}

SetupLogon::SetupLogon(QWidget * parent) : QDialog(parent), ui(new Ui::SetupLogon) {

	ui->setupUi(this);
	Fill();
}

SetupLogon::~SetupLogon() {
	delete ui;
}

void SetupLogon::on_RegLink_clicked() {
	rzd::OpenUrl(QUrl(rzd::Query("RegisterPage")));
}


void SetupLogon::on_ShowPassword_toggled(bool checked) {
	ui->Password->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
}

void SetupLogon::on_buttonBox_accepted() {
	SetupLogonSettings.setValue("SetupLogon/ShowPassword", ui->ShowPassword->isChecked());
	SetupLogonSettings.setValue("SetupLogon/RememberPassword", ui->RememberPassword->isChecked());
	SetupLogonSettings.setValue("SetupLogon/Login", ui->RememberPassword->isChecked() ? ui->Login->text() : "");
	SetupLogonSettings.setValue("SetupLogon/Password", ui->RememberPassword->isChecked() ? ui->Password->text() : "");
}

void SetupLogon::Fill() {
	ui->ShowPassword->setChecked(SetupLogonSettings.value("SetupLogon/ShowPassword", false).toBool());
	ui->RememberPassword->setChecked(SetupLogonSettings.value("SetupLogon/RememberPassword", false).toBool());
	if (ui->RememberPassword->isChecked()) {
		ui->Login->setText(SetupLogonSettings.value("SetupLogon/Login", "").toString());
		ui->Password->setText(SetupLogonSettings.value("SetupLogon/Password", "").toString());
	}
}

void SetupLogon::showEvent(QShowEvent *se) {
	Fill();
}

QString SetupLogon::Login() {
	return ui->Login->text();
}

QString SetupLogon::Password() {
	return ui->Password->text();
}

bool SetupLogon::IsValid() {
	return (Login().isEmpty() == false) && (Password().isEmpty() == false);
}
